import axios from 'axios';
import { notification } from 'antd';
import store from '../redux/index';

// 请求拦截
axios.interceptors.request.use((config) => {
    // const token = window.sessionStorage.getItem('token');
    if (store.getState().token) config.headers.token = store.getState().token;
    return config;
});

// 响应拦截
axios.interceptors.response.use((response) => {
    console.log('response', response);

    if (response.data.code === 401) {
        window.location.href = '/login';
    }

    if (response.data.code === 400) {
        notification.error({ message: response.data.message });
    }

    return response.data;
});

export default axios;
